package com.niknik.woody.utils.wunit.junit.rule;

import com.google.inject.Inject;
import org.junit.rules.ErrorCollector;
import org.junit.rules.ExternalResource;
import org.junit.runner.Description;
import org.junit.runners.model.Statement;
import org.mockito.InOrder;

import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.Callable;

/**
 * User: yoda
 * Date: 10.12.11
 * Time: 14:12
 */

@Deprecated
public class AfterTestVerifier
    extends ExternalResource {

  @Inject private InOrder inOrder;

  private ErrorCollector exCollector = new ErrorCollector();

  private List<Callable> checkFirst = new LinkedList<Callable>();

  @Override
  protected void after() {
    super.after();
    for (Callable callable : checkFirst) {
      exCollector.checkSucceeds(callable);
    }
    exCollector.checkSucceeds(new Callable<Object>() {

      @Override
      public Object call()
          throws Exception {
        inOrder.verifyNoMoreInteractions();
        return true;
      }
    });
  }

  public void check(Callable callable) {
    checkFirst.add(callable);
  }

  @Override
  public Statement apply(Statement base, final Description description) {
    final Statement externalResourceStmt = super.apply(base, description);
    final Statement exCollectorStmt = exCollector.apply(externalResourceStmt, description);

    return new Statement() {

      @Override
      public void evaluate()
          throws Throwable {
        exCollectorStmt.evaluate();
      }
    };
  }
}